<template>
  <!-- 面包屑 子组件-->
  <div>
    <img
      class="currposition-img"
      src="../../assets/images/home/current-position.png"
      alt=""
    />
    <span class="currposition-font">{{ $t("当前位置") }}：</span>
    <el-breadcrumb class="app-breadcrumb" separator="/">
      <transition-group name="breadcrumb">
        <el-breadcrumb-item
          v-for="(item, index) in levelList"
          :key="item.path"
          class="breadcrumb-item"
        >
          <span
            v-if="
              item.redirect === 'noRedirect' || index == levelList.length - 1
            "
            class="no-redirect"
            >{{ $t(item.meta.title) }}</span
          >
          <!-- <a v-else @click.prevent="handleLink(item)">{{ item.meta.title }}</a> -->
        </el-breadcrumb-item>
      </transition-group>
    </el-breadcrumb>
    <span v-if="showBack" @click="goBack" class="back-area cursor-pointer">
      <img src="../../assets/images/back.png" alt="" align="center" />
      <span class="back">{{ $t("返回") }}</span>
    </span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      levelList: null,
      showBack: false,
      hasBackRoutes: [
        "/template/addTemplate",
        "/template/editTemplate",
        "/manage/addManage",
        "/manage/editManage",
        "/inspectrecords/object",
        "/inspectrecords/dimensionality",
        "/inspectrecords/dimensionalityObj",
        "/details",
      ],
    };
  },
  watch: {
    $route: {
      // if you go to the redirect page, do not update the breadcrumbs
      handler(route) {
        if (route.path.startsWith("/redirect/")) {
          return;
        }
        this.getBreadcrumb();
        this.showBack = this.hasBackRoutes.some((item) => {
          console.log(item);
          return item == this.$route.path;
        });
      },
      immediate: true,
    },
  },
  computed: {},
  created() {
    this.getBreadcrumb();
  },
  methods: {
    getBreadcrumb() {
      let matched = this.$route.matched.filter(
        (item) => item.meta && item.meta.title
      );
      const first = matched[0];
      if (!this.isDashboard(first)) {
        matched = [
          { path: "/robot/monitor", meta: { title: this.$t("机器监控") } },
        ].concat(matched);
      }
      this.levelList = matched.filter(
        (item) => item.meta && item.meta.title && item.meta.breadcrumb !== false
      );
    },

    isDashboard(route) {
      const name = route && route.name;
      if (!name) {
        return false;
      }
      return name.trim() === "/robot/monitor";
    },
    handleLink(item) {
      const { redirect, path } = item;
      if (redirect) {
        this.$router.push(redirect);
        return;
      }
      this.$router.push(path);
    },
    goBack() {
      this.$router.go(-1);
    },
  },
};
</script>

<style lang="scss" scoped>
.app-breadcrumb.el-breadcrumb {
  position: relative;
  display: inline-block;
  // line-height: 50px;
  padding: 15px 0 11px;

  .no-redirect {
    cursor: text;
    span :nth-child(1) {
      display: none !important;
    }
  }
}

.el-breadcrumb__item {
  float: none !important;
}
// 图片样式
.currposition-img {
  width: 18px;
  height: 18px;
  vertical-align: middle;
}
// 字体颜色
.currposition-font {
  display: inline-block;
  margin-left: 15px;
  font-size: 14px;
  color: #fff;
}
.el-breadcrumb ::v-deep .el-breadcrumb__inner {
  color: #fff !important;
}
>>> .breadcrumb-item {
  > span {
    &:nth-of-type(1) {
      display: none;
    }
  }
}
.back-area {
  position: absolute;
  right: 50px;
  top: 75px;
  z-index: 1;
  img {
    width: 20px;
    height: 20px;
  }
  .back {
    font-size: 16px;
    font-family: SourceHanSansSC-Regular, SourceHanSansSC;
    font-weight: 400;
    color: #00d1ff;
    line-height: 18px;
    margin-left: 6px;
  }
}
::v-deep {
  .app-breadcrumb .breadcrumb-item:nth-child(1) .el-breadcrumb__separator {
    display: none !important;
  }
}
</style>
